﻿using UnityEngine;
using UnityEditor;
using FrameOperateSync;

static class SyncFrameSetting
{
    private static SyncFrameConfig mOpConfig;
    private static SerializedObject mSer_OpConfig;
    private static SerializedProperty siglePlayerMode;
    private static SerializedProperty frameDuration;
    private static SerializedProperty buffCount;
    private static SerializedProperty logLagFlag;
    private static SerializedProperty logCoreFlag;
    private static SerializedProperty networkLagSimulate;
    private static SerializedProperty networkLagTime;
    private static SerializedProperty networkWaveMax;
    [SettingsProvider]
    public static SettingsProvider 操作同步()
    {

        return new SettingsProvider("Project/OperateSync", SettingsScope.Project)
        {
            label = "帧同步设置",
            activateHandler = (searchContext, rootElement) =>
            {
                mOpConfig = SyncFrameConfig.GetConfig();
                mSer_OpConfig = new SerializedObject(mOpConfig);
                siglePlayerMode = mSer_OpConfig.FindProperty("siglePlayerMode");
                frameDuration = mSer_OpConfig.FindProperty("frameDuration");
                buffCount = mSer_OpConfig.FindProperty("buffCount");
                logLagFlag = mSer_OpConfig.FindProperty("logLagInfoFlag");
                logCoreFlag = mSer_OpConfig.FindProperty("logCoreInfoFlag");
                networkLagSimulate = mSer_OpConfig.FindProperty("networkLagSimulate");
                networkLagTime = mSer_OpConfig.FindProperty("networkLagTime");
                networkWaveMax = mSer_OpConfig.FindProperty("networkWaveMax");
                siglePlayerMode.boolValue = mOpConfig.siglePlayerMode;
                frameDuration.intValue = mOpConfig.frameDuration;
                buffCount.intValue = mOpConfig.buffCount;
                logLagFlag.boolValue = mOpConfig.logLagInfoFlag;
                logCoreFlag.boolValue = mOpConfig.logCoreInfoFlag;
                networkLagSimulate.boolValue = mOpConfig.networkLagSimulate;
                networkLagTime.intValue = mOpConfig.networkLagTime;
                networkWaveMax.intValue = mOpConfig.networkWaveMax;
            },
            guiHandler = (searchContext) => 
            {
                EditorGUILayout.BeginVertical(GUILayout.MaxHeight(500));
                EditorGUILayout.PropertyField(siglePlayerMode, new GUIContent("单人模式"));
                EditorGUILayout.PropertyField(frameDuration, new GUIContent("逻辑帧"));
                EditorGUILayout.PropertyField(buffCount, new GUIContent("buff"));
                EditorGUILayout.PropertyField(logLagFlag, new GUIContent("延迟日志"));
                EditorGUILayout.PropertyField(logCoreFlag, new GUIContent("核心状态日志"));
                EditorGUILayout.PropertyField(networkLagSimulate, new GUIContent("模拟网络延迟"));
                EditorGUILayout.PropertyField(networkLagTime, new GUIContent("网络延迟（ms）"));
                EditorGUILayout.PropertyField(networkWaveMax, new GUIContent("网络波动峰值（ms）"));
                EditorGUILayout.EndVertical();
            }, 
            deactivateHandler = () => 
            {
                if(mOpConfig != null)
                {
                    mOpConfig.siglePlayerMode = siglePlayerMode.boolValue;
                    mOpConfig.frameDuration = frameDuration.intValue;
                    mOpConfig.buffCount = buffCount.intValue;
                    mOpConfig.logLagInfoFlag = logLagFlag.boolValue;
                    mOpConfig.logCoreInfoFlag = logCoreFlag.boolValue;
                    mOpConfig.networkLagSimulate = networkLagSimulate.boolValue;
                    mOpConfig.networkLagTime = networkLagTime.intValue;
                    mOpConfig.networkWaveMax = networkWaveMax.intValue;
                    EditorUtility.SetDirty(mOpConfig);
                }
            },
            
        };
    }
}
